package com.jdjw.modules.abuttingjoint.timing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.thirdpost.service.LaborEmploymentServiceImpl;
import com.jdjw.modules.thirdpost.service.MigrantSettlementServiceImpl;
import com.jdjw.modules.thirdpost.service.ProjectPayPersonnelServiceImpl;
import com.jdjw.modules.thirdpost.service.ProjectServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TimedTask implements Runnable{

    static SimpleDateFormat sjgs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 定时任务
     * 1点执行，执行一次
     */
    public static void timer4() {

        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
        long oneDay = 24 * 60 * 60 * 1000;
        long initDelay  = getTimeMillis("01:00:00") - System.currentTimeMillis();
        initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;

        executor.scheduleAtFixedRate(
                new TimedTask(),
                1,
                1000,
                TimeUnit.MILLISECONDS);
    }

    /**
     * 获取指定时间对应的毫秒数
     * @param time "HH:mm:ss"
     * @return
     */
    private static long getTimeMillis(String time) {
        try {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
            Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
            return curDate.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //如果出现问题，就会返回：01:00:00  这个时间的毫秒值
        return 1625331600000L;
    }


    /**
     * //获取昨天日期：年月日
     * @return
     */
    public String time(){
        Date d=new Date(System.currentTimeMillis()-1000*60*60*24);
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
        String time = sp.format(d);
        return time;
    }


    /**
     * 线程任务的执行体
     */
    @Override
    public void run(){
        //LaborEmploymentServiceImpl.doPostLaborEmployment();

        //MigrantSettlementServiceImpl.doPostMigrantSettlement();

        //ProjectServiceImpl.doPostProject();
        //ProjectServiceImpl.doPostProjectContract();
        //ProjectServiceImpl.doPostProjectTender();
        //ProjectPayPersonnelServiceImpl.doPostPay();
        //ProjectPayPersonnelServiceImpl.doPostCompany();
//        //获取昨天日期
//        Date d = new Date(System.currentTimeMillis()-1000*60*60*24);
//        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
//        String time = sp.format(d);
//        Date date = new Date();
//        String dateDq = sjgs.format(date);
//
//        DwdZchlbtJiandeServiceImpl.doPostTestTwoa(time,dateDq);
//
//        DwdWsgkJiandeServiceImpl.doPostTestTwob(time,dateDq);
//
//        DwdTkrymdMzJiandeServiceImpl.doPostTestTwoc(time,dateDq);
//
//        DwdSzZyxzcmxbJiandeServiceImpl.doPostTestTwod(time,dateDq);
//
//        DwdSzZczyfbhtsjkmxJiandeServiceImpl.doPostTestTwoe(time,dateDq);
//
//        DwdSzZczyczxxdjbJiandeServiceImpl.doPostTestTwof(time,dateDq);
//
//        DwdSzFbhtyjkmxJiandeServiceImpl.doPostTestTwog(time,dateDq);
//
//        DwdQzsqJiandeServiceImpl.doPostTestTwoh(time,dateDq);
//
//        DwdLswgJiandeServiceImpl.doPostTestTwoi(time,dateDq);
//
//        DwdKncjrshbtJiandeServiceImpl.doPostTestTwoj(time,dateDq);
//
//        DwdGlbtffsjJiandeServiceImpl.doPostTestTwos(time,dateDq);
//
//        DwdGemdJiandeServiceImpl.doPostTestTwor(time,dateDq);
//
//        DwdGcRyjgJiandeServiceImpl.doPostTestTwop(time,dateDq);
//
//        DwdGcJbxxJiandeServiceImpl.doPostTestTwoo(time,dateDq);
//
//        DwdGcFkxxJiandeServiceImpl.doPostTestTwoq(time,dateDq);
//
//        DwdFpwkJiandeServiceImpl.doPostTestTwon(time,dateDq);
//
//        DwdDbjffmdJiandeServiceImpl.doPostTestTwom(time,dateDq);
//
//        DwdDbdbjtcymdMzJiandeServiceImpl.doPostTestTwol(time,dateDq);
//
//        DwdCjcgJiandeServiceImpl.doPostTestTwok(time,dateDq);

    }


    /**
     * HttpEntity发送post请求
     * @param url
     * @param list
     * @return
     */
    public static ResponseResult getHttpResponseJson(String url, ArrayList list) {
        // 响应模型
        CloseableHttpResponse response = null;
        HttpEntity responseEntity = null;
        ResponseResult stuCode = null;

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 创建Post请求
        HttpPost httpPost = new HttpPost(url);

        StringEntity entity = new StringEntity(JSON.toJSONString(list), "UTF-8");

        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        httpPost.setEntity(entity);

        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            responseEntity = response.getEntity();

            if (responseEntity != null) {
                stuCode = JSONObject.parseObject(EntityUtils.toString(responseEntity),ResponseResult.class);

                return stuCode;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("http发送post数据失败");
        } finally {
            try {
                // 释放资源
                if (response != null) {
                    response.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                log.info("失败: IOException");
            }
        }
        return stuCode;
    }



}

